package com.lbn.monitor.base;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;

import com.free.framework.base.BaseActivity;
import com.free.framework.lib.utils.Dp2Px;
import com.free.framework.mvp.BaseMvpPresenter;
import com.umeng.message.PushAgent;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.header.MaterialHeader;

/**
 * @author 李炳南
 * @date 2017/3/6
 */
public class AppBaseActivity <P extends BaseMvpPresenter>extends BaseActivity<P> {

    private PtrFrameLayout ptrFrameLayout ;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
//        PushAgent.getInstance(this).onAppStart();
    }


    protected void initPtr(final PtrFrameLayout ptrFrameLayout, final OnPtrRefresh onPtrRefresh){
        initPtr(ptrFrameLayout,onPtrRefresh,true);
    }

    protected void initPtr(final PtrFrameLayout ptrFrameLayout, final OnPtrRefresh onPtrRefresh,boolean auto){
        this.ptrFrameLayout = ptrFrameLayout;
        MaterialHeader header = new MaterialHeader(ptrFrameLayout.getContext());

        int vPadding = Dp2Px.dip2px(ptrFrameLayout.getContext(), 10f);

        header.setPadding(0, vPadding, 0, vPadding);
        ptrFrameLayout.setDurationToCloseHeader(1500);
        ptrFrameLayout.setHeaderView(header);
        ptrFrameLayout.addPtrUIHandler(header);

        ptrFrameLayout.setLoadingMinTime(1000);
        ptrFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                onPtrRefresh.onPtrRefresh();
            }
        });

        if(auto){
            ptrFrameLayout.post(new Runnable() {
                @Override
                public void run() {
                    ptrFrameLayout.autoRefresh();
                }
            });
        }

    }


    public interface OnPtrRefresh{

       void onPtrRefresh();

    }


    @Override
    public void hideLoadingView() {
        super.hideLoadingView();
        if(ptrFrameLayout!=null){
            ptrFrameLayout.refreshComplete();
        }
        hideLoading();
    }


    public void hideLoading(){

    }

    @Override
    public void hideRefreshingView() {
        super.hideRefreshingView();
        if(ptrFrameLayout!=null){
            ptrFrameLayout.refreshComplete();
        }
        hideLoading();
    }
}
